/******************************************************************************
* Copyright (C) 2020 ShenZhen Powerdata Information Technology Co.,Ltd
* All Rights Reserved.
* 本软件为***研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
* 复制、修改或发布本软件.
*****************************************************************************/

package com.xgf.common.xgfcommon.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
* @Title: Date 公共类
* @ClassName:  DateUtils   .
* @Description:TODO  
* @author: xgf
* @date:   2020年2月7日 上午8:50:59   
* @since   JDK1.8
* @history 2020年2月7日 TODO 新建
*/
@Slf4j
public class DateUtils {

	public static void main(String[] args) {
		String format = format(new Date());
		System.out.println(format);
	}
	/**
	 * date 转 String 默认年月日  2020-02-07
	* @Title: format
	* @Description: 性能相差2倍
	* @param @param date 入参：Date
	* @param @return 
	* @return String 2020-02-07 年月日
	* @throws
	 */
	public static String format(Date date) {
		DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		LocalDateTime now = dateToLocalDate(date);
		return ofPattern.format(now);
	}
	
	/**
	 * date 转 dateToLocalDate
	* @Title: dateToLocalDate
	* @Description: TODO
	* @param @param date
	* @param @return 
	* @return LocalDateTime
	* @throws
	 */
	public static LocalDateTime dateToLocalDate(Date date) {
		Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
	}
	
	/**
	 * 
	 * @param nowTime   当前时间	
	 * @param startTime	开始时间
	 * @param endTime   结束时间
	 * @return
	 * @author sunran   判断当前时间在时间区间内
	 */
	public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
	/**
	 * 添加几天的时间
	* @Title: addDay
	* @Description: TODO
	* @param @param date 最初时间
	* @param @param day 添加减少的时间
	* @param @return 
	* @return Date
	* @throws
	 */
	public static Date addDay(Date date, int day) {
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        c.add(Calendar.DAY_OF_MONTH, day);  
        return c.getTime();
	}
	
	public static List<String> getDateListByBetweenTime(List<String> list, Date date, int day) {
		List<String> resultList = new ArrayList<>();
		Date addDay = addDay(date, day);
		Date addDayNext = addDay(date, day + 1);
		
		SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (String dateString : list) {
			Date parse = null;
			try {
				parse = simpleDateFormat2.parse(dateString);
			} catch (ParseException e) {
				log.error(e.toString());
			}
			
			boolean effectiveDate = isEffectiveDate(parse,addDay,  addDayNext);
			if(effectiveDate) {
				resultList.add(dateString);
			}
		}
		return resultList;
		
	}
}
